package com.batch.job;

import com.batch.incrementer.MyTimeStampParamIncrementer;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;

import java.util.Map;


// 主要学习熟悉 作业唯一标识 的概念(学习章节2.2)
// 学习当前Job，请将@SpringBootApplication 取消注释，并将其他Job的@SpringBootApplication 方法注释,否则会对当前结果产生干扰


/**
 * 作业自增参数：
 *  在job定义时，加入自增器： .incrementer(new RunIdIncrementer())
 *  那么Spring 会自动在 JobParameter 中加入参数 run.id 并自增
 *
 * 作业时间戳参数：
 *  1、需要自定义时间戳增量器，实现 JobParametersIncrementer
 *  2、加入自增器：   .incrementer(new MyTimeStampParamIncrementer())
 */


//@SpringBootApplication
// 因为用的Step路径在别的目录下，因此需要手动加ComponentScan
@ComponentScan(basePackages = "com.batch",
        excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.batch.job"))
// EnableBatchProcessing 使spring创建springbatch操作相关类对象
@EnableBatchProcessing
public class ParamJob2 {

    @Autowired
    private ApplicationContext context;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    // Job构建工厂，用于构建Job对象
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    // JobParameter
    private JobParameter jobParameter;

    @Bean
    public Tasklet paramTasklet2_01() {
        return (StepContribution contribution, ChunkContext chunkContext) -> {
            Map<String, Object> jobParameters = chunkContext.getStepContext().getJobParameters();
            Object runId = jobParameters.get("run.id");
            System.out.println("chunkContext方式");
            System.out.println("获取的jobParameters为:"+jobParameters.toString());
            System.out.println("run.id:"+runId);
            return RepeatStatus.FINISHED;
        };
    }

    @Bean
    public Tasklet paramTasklet2_02() {
        return (StepContribution contribution, ChunkContext chunkContext) -> {
            Map<String, Object> jobParameters = chunkContext.getStepContext().getJobParameters();
            Object runId = jobParameters.get("daily");
            System.out.println("chunkContext方式");
            System.out.println("获取的jobParameters为:"+jobParameters.toString());
            System.out.println("daily:"+runId);
            return RepeatStatus.FINISHED;
        };
    }

    @Bean
    public Step paramStep2_01(){
        return stepBuilderFactory.get("param-step2-01").tasklet(paramTasklet2_01()).build();
    }

    @Bean
    public Step paramStep2_02(){
        return stepBuilderFactory.get("param-step2-02").tasklet(paramTasklet2_02()).build();
    }


    @Bean
    public Job paramJob2_01() {
        return jobBuilderFactory.get("param-job2-01").start(paramStep2_01())
                .incrementer(new RunIdIncrementer())
                .build();
    }

    @Bean
    public Job paramJob2_02() {
        return jobBuilderFactory.get("param-job2-02").start(paramStep2_02())
                .incrementer(new MyTimeStampParamIncrementer())
                .build();
    }


    public static void main(String[] args) {
        SpringApplication.run(ParamJob2.class, args);
    }


}
